<?php
class Model_User extends Model 
{
	/**
	 * 用户组的hash数组
	 * @var array 1d 
	 */
	public  $hashGroup;
	
    function __construct(){
    	parent::__construct();
	$this->hashGroup = M('UserGroup')->getHash();

    }
	/**
	 * 在保存数据之前检查是否改变了密码字段
	 * @param array $data
	 * @return mix 如果为Insert则返回列的id号，如果为Update则返回受影响的
	 */
	public  function save($data){
		if(empty($data['password'])){
			unset($data['password']);
		} else{
			$data['password'] = md5($data['password']);
		}
		return parent::save($data);
	}
	
	
	protected function _after_select_fetch(&$row){
		isset($row['groupid']) && $row['_group']= $this->hashGroup[$row['groupid']];
	}
	
	public function getAllUser(){
		static $_cache = null;
		if ($_cache == null) {
			$data = $this->select("`uid`,`groupid`, `employeenumber`,`username`","order by `employeenumber`");
			foreach ($data as $datum){
					$_cache[$datum['_group']][$datum['uid']] = "[{$datum['employeenumber']}] {$datum['username']}";
			}
		}
		return $_cache;
	}
}
?>